<?php

//$toemail = $argv[0];

$stamp = date('d-m-Y--H-i');
$file = 'virusscan-'.$stamp.'.txt';
$fullpath1 = '/usr/local/psa/admin/htdocs/modules/clamav/logs/'.$file;
$fullpath2 = '/usr/local/psa/admin/htdocs/modules/clamav/logs/'.$file;

$list = file_get_contents('/usr/local/psa/var/modules/clamav/scanlist.dat');
if (!empty($list)){
exec('/usr/bin/freshclam > /dev/null 2>&1');
sleep(5);
exec('/usr/bin/clamscan -r --file-list=/usr/local/psa/var/modules/clamav/scanlist.dat --log=/usr/local/psa/admin/htdocs/modules/clamav/logs/'.$file .'> /dev/null 2>&1');
//copy($fullpath2,$fullpath1);
//chmod($fullpath1,0644);
//chown($fullpath1,'nobody');
//chgrp($fullpath1,'nobody');
//unlink($fullpath2);

// need to take in email address arugment test if added
/**
$scanResults = file_get_contents($fullpath1);
if(strpos($scanResults , "FOUND")) {
    $sresult = "Virus Found";   


$my_file = $file;
$my_path = "/usr/local/psa/admin/htdocs/modules/clamav/";
$my_name = "ExtendPlesk ClamScan Agent";
$my_mail = "clamscan@".$_SERVER['HOST_NAME'];
$my_replyto = "noreply@".$_SERVER['HOST_NAME'];
$my_subject = "This is a ClamScan Report: ". $sresult ;
$my_message = "Hello Administrator,\r\nThere was a virus found on one of you hosted sites please find the scan results below. \r\nPowered by ExtendPlesk.com\r\n\r\ngr. ".$scanResults;

$toemail = "tommyz4@gmail.com";
if (VerifyMailAddress($toemail) == true) {
	mail_attachment($my_file, $my_path, $toemail , $my_mail, $my_name, $my_replyto, $my_subject, $my_message);
}


function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
    $file = $path.$filename;
    $file_size = filesize($file);
    $handle = fopen($file, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $content = chunk_split(base64_encode($content));
    $uid = md5(uniqid(time()));
    $name = basename($file);
    $header = "From: ".$from_name." <".$from_mail.">\r\n";
    $header .= "Reply-To: ".$replyto."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $message."\r\n\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";
    $header .= "--".$uid."--";
    if (mail($mailto, $subject, "", $header)) {
        echo "mail send ... OK"; // or use booleans here
    } else {
        echo "mail send ... ERROR!";
    }
}


}


function VerifyMailAddress($address) {
   $Syntax='#^[w.-]+@[w.-]+.[a-zA-Z]{2,5}$#';
   if(preg_match($Syntaxe,$adrdess))
      return true;
   else
     return false;
}
**/

}
?>